package com.studyyoun.android.nonghe.share;

import android.app.*;
import android.os.*;
import android.text.TextUtils;

import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.home.model.ArtModel;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;
import com.studyyoun.android.nonghe.msg.model.MsgModel;
import com.tencent.connect.share.*;
import com.tencent.tauth.*;

import java.util.*;

import static com.studyyoun.android.nonghe.share.QQMainFunction.mTencent;

/**
 * Created by androidlongs on 17/6/15.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

public class QQShareFunction {
    private QQShareFunction() {

    }

    private static class QQShareSingle {
        private static QQShareFunction sQQShareFunction = new QQShareFunction();
    }

    public static QQShareFunction getInstance() {
        return QQShareSingle.sQQShareFunction;
    }


    private int shareType = QQShare.SHARE_TO_QQ_TYPE_DEFAULT;


    public void shareArtPicAndMsgToQQ(final Activity activity, ArtModel articleModel) {

        if (articleModel == null) {
            return;
        }

        String artName = articleModel.artName + " 摘自《智慧安卓》";
        String artFalg = articleModel.artFalg;

        String artImageUrl = articleModel.artImageUrl;
        artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, artName);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "" + artFalg);
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, HttpUrlHelper.sArtShareUrl + "?id=" + articleModel.id);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, artImageUrl);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "《智慧安卓》文章分享 ");

        IUiListener qqShareListener = new IUiListener() {
            @Override
            public void onCancel() {
                if (shareType != QQShare.SHARE_TO_QQ_TYPE_IMAGE) {
                    LogUtils.i("取消QQ分享");
                    ToastUtils.show("取消分享", activity);
                }
            }

            @Override
            public void onComplete(Object response) {
                LogUtils.d("分享完成 " + response.toString());
                ToastUtils.show("分享完成", activity);
            }

            @Override
            public void onError(UiError e) {
                // TODO Auto-generated method stub
                LogUtils.e("分享异常 " + e.errorCode + " \n message " + e.errorMessage);
                ToastUtils.show("分享异常，请重新进入应用", activity);
            }
        };


        if (null != mTencent) {
            mTencent.shareToQQ(activity, params, qqShareListener);
        }

    }

    public void shareArtPicAndMsgToQQQzone(final Activity activity, ArtModel articleModel) {
        if (articleModel == null) {
            return;
        }

        String artName = articleModel.artName + " 摘自《智慧安卓》";
        String artFalg = articleModel.artFalg;

        String artImageUrl = articleModel.artImageUrl;
        artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

        final Bundle params = new Bundle();
        //分享类型
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, artName);//必填
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, artFalg);//选填
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, HttpUrlHelper.sArtShareUrl + "?id=" + articleModel.id);//必填
        ArrayList<String> imageUrls = new ArrayList<String>();
        imageUrls.add(artImageUrl);
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);

        IUiListener qqShareListener = new IUiListener() {
            @Override
            public void onCancel() {
                if (shareType != QQShare.SHARE_TO_QQ_TYPE_IMAGE) {
                    LogUtils.i("取消QQ分享");
                    ToastUtils.show("取消分享", activity);
                }
            }

            @Override
            public void onComplete(Object response) {
                LogUtils.d("分享完成 " + response.toString());
                ToastUtils.show("分享完成", activity);
            }

            @Override
            public void onError(UiError e) {
                // TODO Auto-generated method stub
                LogUtils.e("分享异常 " + e.errorCode + " \n message " + e.errorMessage);
                ToastUtils.show("分享异常，请重新进入应用", activity);
            }
        };
        if (null != mTencent) {
            mTencent.shareToQzone(activity, params, qqShareListener);
        }
    }

    public void shareMsgAndMsgToQQQzone(final Activity activity, MsgModel articleModel) {
        if (articleModel == null) {
            return;
        }

        String artName = articleModel.getMsgTitle() + " 摘自《智慧安卓》";
        String artFalg = articleModel.getMsgRemark();

        String artImageUrl =articleModel.getImgUrl();
        String msgModelImgUrl = articleModel.getImgUrl();
        if (!TextUtils.isEmpty(msgModelImgUrl)) {
            if (msgModelImgUrl.contains(",")) {
                String[] imageUrlSplit = msgModelImgUrl.split(",");
                if (imageUrlSplit != null && imageUrlSplit.length > 0) {
                    artImageUrl = imageUrlSplit[0];
                }
            }
        }
        artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

        final Bundle params = new Bundle();
        //分享类型
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, artName);//必填
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, artFalg);//选填
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, HttpUrlHelper.sMsgShareUrl + "?id=" + articleModel.getId());//必填
        ArrayList<String> imageUrls = new ArrayList<String>();
        imageUrls.add(artImageUrl);
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);

        IUiListener qqShareListener = new IUiListener() {
            @Override
            public void onCancel() {
                if (shareType != QQShare.SHARE_TO_QQ_TYPE_IMAGE) {
                    LogUtils.i("取消QQ分享");
                    ToastUtils.show("取消分享", activity);
                }
            }

            @Override
            public void onComplete(Object response) {
                LogUtils.d("分享完成 " + response.toString());
                ToastUtils.show("分享完成", activity);
            }

            @Override
            public void onError(UiError e) {
                // TODO Auto-generated method stub
                LogUtils.e("分享异常 " + e.errorCode + " \n message " + e.errorMessage);
                ToastUtils.show("分享异常，请重新进入应用", activity);
            }
        };
        if (null != mTencent) {
            mTencent.shareToQzone(activity, params, qqShareListener);
        }
    }

    public void shareMsgAndMsgToQQ(final Activity activity, MsgModel articleModel) {

        if (articleModel == null) {
            return;
        }

        String artName = articleModel.getMsgTitle() + " 摘自《智慧安卓》";
        String artFalg = articleModel.getMsgRemark();

        String
                artImageUrl = articleModel.getImgUrl();
        String msgModelImgUrl = articleModel.getImgUrl();
        if (!TextUtils.isEmpty(msgModelImgUrl)) {
            if (msgModelImgUrl.contains(",")) {
                String[] imageUrlSplit = msgModelImgUrl.split(",");
                if (imageUrlSplit != null && imageUrlSplit.length > 0) {
                    artImageUrl = imageUrlSplit[0];
                }
            }
        }
        artImageUrl = HttpUrlHelper.BaseImageHost + artImageUrl;

        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, artName);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "" + artFalg);
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, HttpUrlHelper.sMsgShareUrl + "?id=" + articleModel.getId());
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, artImageUrl);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "《智慧安卓》文章分享 ");

        IUiListener qqShareListener = new IUiListener() {
            @Override
            public void onCancel() {
                if (shareType != QQShare.SHARE_TO_QQ_TYPE_IMAGE) {
                    LogUtils.i("取消QQ分享");
                    ToastUtils.show("取消分享", activity);
                }
            }

            @Override
            public void onComplete(Object response) {
                LogUtils.d("分享完成 " + response.toString());
                ToastUtils.show("分享完成", activity);
            }

            @Override
            public void onError(UiError e) {
                // TODO Auto-generated method stub
                LogUtils.e("分享异常 " + e.errorCode + " \n message " + e.errorMessage);
                ToastUtils.show("分享异常，请重新进入应用", activity);
            }
        };


        if (null != mTencent) {
            mTencent.shareToQQ(activity, params, qqShareListener);
        }

    }
}
